package com.game.action;

import com.coment.po.UserPet;
import com.coment.vo.UserPetListItem;
import com.game.dao.user.UserPlayInfoDao;
import com.game.dao.user.pet.UserPetDao;
import com.game.logic.items.pet.PetService;
import com.xs.action.annotate.ActionController;
import com.xs.action.annotate.ActionMethod;
import com.xs.action.exection.ActionException;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/// 8-宠物操作
@RestController
@RequestMapping("/petAction")
@ActionController(value = 8, name = "宠物行为")
public class PetAction {
    @Resource
    private PetService petService;
    @Resource
    private UserPetDao userPetDao;
    @Resource
    private UserPlayInfoDao userPlayInfoDao;

    /**
     * 1.查询宠物
     *
     * @param userInfoId 用户id
     * @param petId      宠物id
     */
    @RequestMapping("/select")
    @ActionMethod(value = 1, name = "查询宠物")
    public UserPet select(
            Integer userInfoId,
            int petId
    ) {
        return userPetDao.findUserPetByUserInfoIdAndPetId(userInfoId, petId).orElseThrow(
                () -> new ActionException("没有此宠物")
        );
    }

    /**
     * 2.宠物出战
     *
     * @param userInfoId 用户id
     * @param petId      宠物id
     */
    @RequestMapping("/fight")
    @ActionMethod(value = 2, name = "宠物出战")
    public UserPet fight(
            Integer userInfoId,
            int petId
    ) {
        return petService.fight(userInfoId, petId);
    }

    /**
     * 3.宠物升级
     *
     * @param userInfoId 用户id
     * @param petId      宠物id
     */
    @RequestMapping("/levelUp")
    @ActionMethod(value = 3, name = "宠物升级")
    public UserPet levelUp(
            Integer userInfoId,
            int petId
    ) {
        return petService.petLevelUp(userInfoId, petId);
    }

    /**
     * 4.宠物列表
     *
     * @param userInfoIds 用户id列表
     */
    @GetMapping("/list")
    @ActionMethod(value = 4, name = "宠物列表")
    public List<UserPetListItem> list(Integer[] userInfoIds) {
        return java.util.Arrays.stream(userInfoIds)
                .map(userInfoId -> {
                    int petId = userPlayInfoDao.findByUserInfoIdOrCreate(userInfoId).recentlyPlayedPetId;
                    List<UserPet> userPets = userPetDao.searchAllByUserInfoId(userInfoId);
                    return new UserPetListItem(userInfoId, petId, userPets.stream().map(po -> new UserPetListItem.UserPetVo(
                            po.getPetId(),
                            po.getLv(),
                            po.getSkillLV()
                    )).toList());
                })
                .toList();
    }
}
